package com.xzp.demo.web;

import com.xzp.demo.entity.Area;
import com.xzp.demo.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//restController这个注解等价于 Controller + ResponseBody
@RestController
@RequestMapping(value = "/superadmin")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @GetMapping(value = "/listarea")
    private Map<String,Object> listArea(){
        Map<String,Object> modelMap = new HashMap<>();
        List<Area> list = areaService.getAreaList();
        modelMap.put("areaList",list);
        return modelMap;
    }

    @GetMapping("/getareabyid")  //需要接受请求参数
    private Map<String,Object> getAreaById(@RequestParam(value = "id",required = true) Integer id){
        Area area = areaService.getAreaById(id);
        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("area",area);
        return modelMap;
    }

    @PostMapping(value = "/addarea")
    private Map<String,Object> addArea(@RequestBody Area area){
        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("success",areaService.addArea(area));
        return modelMap;
    }

    @PostMapping(value = "/updatearea")
    private Map<String,Object> updateArea(@RequestBody Area area){  //RequestBody接受json参数
        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("success",areaService.updateArea(area));
        return modelMap;
    }

    @RequestMapping(value = "/deletearea",method = RequestMethod.GET)
    private Map<String,Object> deleteArea(@RequestParam(value = "id",required = true) Integer areaId){  //id对应于请求参数
        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("success",areaService.deleteArea(areaId));

        return modelMap;
    }
}
